<?php

namespace app\keeper\controller;

use app\admin\controller\File;
use app\common\model\mysql\ReimburseType;
use app\common\model\mysql\Reimburse as ReimburseModel;

class Reimburse extends AuthBase
{
    //报销类型列表
    public function reimburseType(){
        $where = [
            ['company_id','=',$this->companyId],
            ['isdel','=','0']
        ];
        $reimburseTypeData = (new ReimburseType())->getNormalReimburseTypeList($where,"id as reimburse_type_id,reimburse_type_name");
        return show(config('status.success'), "报销类型列表", $reimburseTypeData);
    }

    //申请报销操作
    public function reimburse(){
        $reimburse_type_id = $this->request->param('reimburse_type_id');
        if(empty($reimburse_type_id)){
            return show(config("status.error"),"缺少参数报销类型id");
        }
        $tmoney = $this->request->param('tmoney');
        if(empty($tmoney)){
            return show(config("status.error"),"缺少参数报销金额");
        }
        $remark = $this->request->param('remark');
        $reimburseImg = $this->request->param('reimburseImg');
        $reimburseTypeData = (new ReimburseType())->getNormalReimburseType($reimburse_type_id,"*");
        $data['company_id'] = $this->companyId;
        $data['shop_id'] = $this->shopId;
        $data['worker_id_apply'] = $this->workerId;
        $data['worker_name_apply'] = $this->workerName;
        $data['applydate'] = date("Y-m-d H:i:s",time());
        $data['reimburse_type_id'] = $reimburse_type_id;
        $data['reimburse_type_name'] = $reimburseTypeData['reimburse_type_name'];
        $data['tmoney'] = $tmoney;
        $data['reimburse_img'] = implode(',', $reimburseImg);
        $data['remark'] = $remark;
        $data['adddate'] = date("Y-m-d H:i:s",time());
        $res = (new ReimburseModel())->addReimburseData($data);
        if($res){
            return show(config("status.success"), "报销申请成功", $res);
        }else{
            return show(config("status.error"),"报销申请失败");
        }
    }

    //报销历史列表
    public function reimburseHistory(){
        $page = $this->request->param('pageNum', 1);//当前页
        $limit = $this->request->param('pageSize', 10);//每页显示条数
        if(empty($page) || empty($limit)){
            return show(config("status.error"),"缺少分页参数");
        }
        $where = [
            ['a.company_id','=',$this->companyId],
            ['a.shop_id','=',$this->shopId],
            ['a.worker_id_apply','=',$this->workerId],
            ['a.isdel','=','0'],
        ];
        $field = "a.id as acc_reimburse_id,a.reimburse_type_name,a.tmoney,a.applydate,a.status,b.tname";
        $HistoryList = (new ReimburseModel())->getNormalReimburseList($where,$limit,$page,$field);
        return show(config("status.success"),"报销历史列表",$HistoryList['data']);
    }

    //报销详情
    public function reimburseDetails(){
        $acc_reimburse_id = $this->request->param('acc_reimburse_id');//每页显示条数
        if(empty($acc_reimburse_id)){
            return show(config("status.error"),"缺少参数报销id");
        }
        $where = [
            ['a.id','=',$acc_reimburse_id],
        ];
        $field = "a.id,a.status,a.reimburse_type_name,b.tname as worker_name_apply,a.applydate,a.tmoney,a.worker_name_check,a.worker_name_pay,a.checkdate,a.remark,a.reimburse_img";
        $list = (new ReimburseModel())->getNormalReimburseFind($where,$field);
        $list['worker_name_check'] = $list['worker_name_check']?:"";
        $list['worker_name_pay'] = $list['worker_name_pay']?:"";
        $list['checkdate'] = $list['checkdate']?:"";
        $reimburse_img = explode(',', $list['reimburse_img']);
        $list['reimburse_img'] = $reimburse_img;
        return show(config("status.success"), "报销详情", $list);
    }

    //上传报销图片
    public function addFileReimburseImgs(){
        $uploadedFiles = (new File())->uploadFiles('images','reimburseImg');
        return show(config("status.success"), "ok", $uploadedFiles);
    }
}
